//
//  AnswerInputBar.swift
//  Game
//
//  Created by woniu on 2025/9/4.
//

import Foundation
import UIKit

class AnswerInputBar: UIView {
    public var onDidChangeBlock: (()->())?
    public var onDidEditFinishBlock: (()->())?
    private var wrap:UIView!
    public var indexLab:UILabel!
    public var inputField:UITextField!
    public var deleteButton:UIButton!
    public var isHiddenItem = false
    
    override init(frame: CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        self.backgroundColor = Colors.blue.withAlphaComponent(0.1)
        let bgColor = Colors.blue
        wrap = UIView()
        wrap.backgroundColor = .white
        wrap.layer.shadowColor = bgColor.withAlphaComponent(0.15).cgColor
        wrap.layer.shadowOpacity = 0.06
        wrap.layer.shadowRadius = 6
        wrap.layer.shadowOffset = CGSize(width: 0, height: 3)
        wrap.borderSet(bgColor, 2)
        self.addSubview(wrap)
        
        indexLab = UILabel()
        indexLab.font = UIFont.boldSystemFont(ofSize: 15)
        indexLab.textAlignment = .center
        indexLab.textColor = Colors.blue
        indexLab.borderSet(bgColor, 0.5)
        indexLab.circular(12.5)
        wrap.addSubview(indexLab)
        
        inputField = UITextField()
        inputField.borderStyle = .none
        inputField.textColor = .black
        inputField.delegate = self
        inputField.placeholder = "候选答案"
        inputField.backgroundColor = .clear
        inputField.textAlignment = .left
        inputField.font = UIFont.boldSystemFont(ofSize: 16)
        wrap.addSubview(inputField)
       
        deleteButton = UIButton(type: .system)
        deleteButton.setImage(UIImage(systemName: "minus.circle"), for: .normal)
        deleteButton.setImage(UIImage(systemName: "eye"), for: .selected)
        deleteButton.tintColor = Colors.blue
        deleteButton.addTarget(self, action: #selector(onDelete), for: .touchUpInside)
        deleteButton.circular(12.5)
        self.addSubview(deleteButton)
    }
    
    @objc func onDelete(){
        deleteButton.isSelected = !deleteButton.isSelected
        isHiddenItem = deleteButton.isSelected
        
        if isHiddenItem{
            inputField.textColor = Colors.disabledColor
            indexLab.textColor = Colors.disabledColor
            inputField.isUserInteractionEnabled = false
            wrap.borderSet(Colors.disabledColor, 2)
            ToastView.show("已删除该项")
        }else{
            
            inputField.textColor = .black
            indexLab.textColor = Colors.blue
            inputField.isUserInteractionEnabled = true
            wrap.borderSet(Colors.blue, 2)
        }
        onDidChangeBlock?()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        wrap.frame = CGRect(x: 10, y: 10, width: self.width-20, height: self.height-20)
        wrap.circular()
        indexLab.frame = CGRect(x: 5, y: (wrap.height-25)*0.5, width: 25, height: 25)
        inputField.frame = CGRect(x: indexLab.frame.maxX+5, y: 0, width: wrap.width-indexLab.frame.maxX-20, height: wrap.height)
        deleteButton.frame = CGRect(x: self.width-10-30, y: (self.height-25)*0.5, width: 25, height: 25)
        
    }
}
extension AnswerInputBar:UITextFieldDelegate{
    
    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
        onDidEditFinishBlock?()
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        onDidEditFinishBlock?()
        return true
    }
}
